import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class Main {

	public static void main(String[] args) {

		final int readres = 100, writters = 10, capacity = 20, iterations = 1000;

		final Random random = new Random(System.nanoTime());

		ArrayList<AbstractLock> locks = new ArrayList<AbstractLock>();
		
	//	locks.add(new ReaderLock());

		//locks.add(new WriterLock());
		
		locks.add(new FifoLock());
		
		for (AbstractLock abstractLock : locks) {

			ArrayList<Thread> list = new ArrayList<Thread>();

			final Lib lib = new Lib(abstractLock, capacity);
			
			lib.setVerbose(false);

			// czytelnicy

			

			for (int i = 0; i < writters; i++) {
				list.add(new Thread("writter" + i) {
					@Override
					public void run() {

						for (int j = 0; j < iterations; j++) {

							int pos = random.nextInt(capacity);

							String s = "nowe: "
									+ Thread.currentThread().getName() + " "
									+ System.nanoTime();

							lib.put(pos, s);

							try {
								TimeUnit.MILLISECONDS.sleep(random.nextInt(100));
							} catch (InterruptedException e) {
								throw new RuntimeException(e);
							}

						}

					}
				});
			}
			
			for (int i = 0; i < readres; i++) {
				list.add(new Thread("reader" + i) {
					@Override
					public void run() {

						
						for (int j = 0; j < iterations; j++) {

							int pos = random.nextInt(capacity);

							lib.read(pos);

							try {
								TimeUnit.MILLISECONDS.sleep(random.nextInt(100));
							} catch (InterruptedException e) {
								throw new RuntimeException(e);
							}

						}

					}
				});
			}
			
			for (Thread thread : list) {
				thread.start();
			}

		}

	}

}
